import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {ColumnType, SPage, TablePageComponent} from "@shared/components/table-page/table-page.component";
import {RoomEditComponent} from "../room-edit/room-edit.component";
import {NzMessageService, NzModalService} from "ng-zorro-antd";
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {ApiService} from "@core/services/api";
import {HttpClient} from "@angular/common/http";

@Component({
  selector: 'app-person-list',
  templateUrl: './person-list.component.html',
  styles: []
})
export class PersonListComponent implements OnInit {
  @Input() set _param(value) {
    this.parma = value;
  };
  parma: any;
  @ViewChild('st', {static: false})st: TablePageComponent;
  config: SPage = {
    columns: [

      {
        index: 'infoName',
        title: '用户名',
        filter: {
          type: 'string'
        }
      },
      {
        index: 'infoSex',
        title: '性别',
        filter: {
          type: 'select',
          items: [
            {label: '男', value:'1'},
            {label: '女', value:'2'},
            {label: '未知', value: '0'},
            {label: '店面', value: '4'}
          ]
        }
      },
      {
        index: 'infoAge',
        title: '年龄',
        filter: {
          type: 'string'
        }
      },
      /*{
        index: 'houseAddress',
        title: '房屋地址',
        filter: {
          type: 'string'
        }
      },*/
      {
        index: 'infoBirthday',
        title: '出生日期'
      },
      {
        index: 'infoNationName',
        title: '民族',
      },
      {
        index: 'infoNativePlace',
        title: '籍贯',
      },
      {
        index: 'infoMobile',
        title: '联系电话',
      },
      /*{
        index: 'infoSex',
        title: '性别',
        filter: {
          type: 'select',
          items: [
            {label: '男', value:'1'},
            {label: '女', value:'2'},
            {label: '未知', value: '0'},
            {label: '店面', value: '4'}
          ]
        }
      },
      {
        index: 'status',
        title: '状态',
        type: ColumnType.tag,
        tag: {
          false: {text:'停用', color: 'red'},
          true: {text:'启用', color: 'green'},
        },
        filter: {
          type: 'select',
          items: [
            {label: '启用', value: true},
            {label: '停用', value: false},
          ]
        }
      },*/
      /*{
        index: 'houseRemark',
        title: '备注'
      },*/
    ],
    reqUrl: '/core/person/page',
    editBtn: [
      {title: '绑定'},
    ],
    ps: 5,
  };

  selectedData: object[];
  editDic(type: 0 | 1 = 0, param?: object): void {
    this.modal.create({
      nzTitle: type === 0 ? '添加房间' : '修改房间',
      nzContent: RoomEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  selectChange(e): void {
    this.selectedData = e;
  }
  operate(e): void {
    console.log(e);
    switch (e.title) {
      case '查看':
        break;
      case '绑定':
        this.addPerson(e.data)
        break
    }
  }
  deleteDic(): void {
    if (!this.selectedData || this.selectedData.length === 0) {
      this.msg.warning('请选择数据后操作');
      return;
    }
    this.modal.confirm({
      nzTitle: '您确定要删除这些数据吗',
      nzOnOk: () => {
        const body = [];
        this.selectedData.forEach((item: any) => {
          body.push({id: item.id})
        })
        this.http.put<any>(this.api.room.delete.url,body).subscribe(res => {
          if (res.status === '200') {
            this.msg.success('删除成功');
            this.st.reload();
          }
        })

      }
    })
  }
  addPerson(person) {
    const body = {
      roomId: this.parma.id,
      personId: person.id,
      roomApprove: 0
    }
    this.http.post('/core/room/person', body).subscribe(res => {
      if (res['status'] == '201') {
        this.msg.success(res['message']);
      }else {
        this.msg.error(res['message'])
      }
    })
  }
  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private area: AreaService,
    private api: ApiService,
    private http: HttpClient
  ) { }

  ngOnInit() {
  }

}
